/**
 * @Author: LiMeng
 * @Date: 2017/3/15
 */
(function ($) {
    /***
     * 序列化表单数据为json格式
     */
    $.fn.serializeJson = function () {
        var serializeObj = {};
        var array = this.serializeArray();
        $(array).each(function () {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return serializeObj;
    };
    /**
     * 获取URL中参数
     * @param name 参数名称
     */
    $.extend({
        getQueryParam: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var str = window.location.search;
            if (str === '') {
                return null;
            }
            var r = str.substr(1).match(reg);
            if (r !== null) {
                return r[2];
            }
            return null;
        },
        getIframeWindow: function () {
            var iframeDom = parent.$('iframe[src*=' + $.getQueryParam('resourceId') + ']:eq(0)');
            if (iframeDom.size() === 1) {
                return iframeDom.get(0).contentWindow
            } else {
                return null;
            }
        },
        generateUUID: function () {
            var d = new Date().getTime();
            return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d / 16);
                return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
            });
        },
        getChineseInteger: function (num) {
            if (!$.isNumeric(num) && num < 0 && num % 1 !== 0 && num > 9) {
                return null;
            }
            var arr = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
            return arr[num];
        },
        getNowFormatDate: function () {
            var addZeroToSingleDigitNumber = function (num) {
                if (!$.isNumeric(num)) {
                    throw '参数格式不正确！';
                }
                if (num < 10 && num >= 0) {
                    return "0" + num;
                }
                return num.toString();
            };
            var date = new Date();
            var month = addZeroToSingleDigitNumber(date.getMonth() + 1);
            var strDate = addZeroToSingleDigitNumber(date.getDate());
            var hours = addZeroToSingleDigitNumber(date.getHours());
            var minutes = addZeroToSingleDigitNumber(date.getMinutes());
            var seconds = addZeroToSingleDigitNumber(date.getSeconds());

            return date.getFullYear() + "-" + month + "-" + strDate
                + " " + hours + ":" + minutes
                + ":" + seconds;
        },
        isNotEmpty: function (str) {
            return !(typeof str === 'undefined'
            || str === null
            || str.toString().replace(/(^s*)|(s*$)/g, "").length === 0);
        },
        now: function () {
            return moment().format('YYYY-MM-DD hh:mm:ss');
        },
        today: function () {
            return moment().format('YYYY-MM-DD');
        }
    });
    /**
     * 获取特定resourceId的tab
     */
    $.ajaxSetup({
        cache: false,//取消缓存
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.alert('数据出现异常！', {
                title: '请求失败',
                icon: 2
            });
        }
    });
    layer.config({
        skin: 'moon',
        anim: 2,
        shade: [0.5, '#ffffff']
    });

    template.config("compress", false);
    var correct = function (str) {
        if (typeof str === 'undefined' || str === null || str.toString().replace(/(^s*)|(s*$)/g, "").length === 0 || str === 'null' || str === 'undefined')
            return '';
        else
            return str;
    };
    template.helper('correct', correct);
})(jQuery);
function taskAdd() {
    var title = '批量任务下发';
    var content = ROOT_PATH + '/task/addTask.shtml?resourceId=' + $.getQueryParam('resourceId');
    parent.layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '70%'],
        content: content
    });
}

$(function () {
    moment.locale('zh-cn');
    /**
     * 表单页面的显示隐藏
     */
    var $pageDown = $('.panel-down');
    if ($pageDown.length !== 0) {
        $pageDown.on('click', function () {
            $(this).parent().next().toggle();
            var $span = $(this).find('span');
            var className = $span.attr('class');
            $span.removeClass();
            if (className === 'fa fa-chevron-down fa-lg') {
                $span.addClass('fa fa-chevron-up fa-lg');
            } else if (className === 'fa fa-chevron-up fa-lg') {
                $span.addClass('fa fa-chevron-down fa-lg');
            }

        });
    }
});
